package com.jindianjichi.auth.appliccation.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jindianjichi.auth.appliccation.convert.AuthUserDTOConvert;

import com.jindianjichi.auth.domain.BO.AuthUserBO;
import com.jindianjichi.auth.domain.service.AuthUserDomainService;

import com.jindianjichi.auth.entity.AuthUserDTO;
import com.jindianjichi.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/user")
public class AuthUserController {
    @Autowired
    private AuthUserDomainService authUserDomainService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.register.dto:{}", JSON.toJSONString(authUserDTO));
        }
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名不能为空");
        AuthUserBO authUserBO = AuthUserDTOConvert.instance.convertToBoAuthUser(authUserDTO);
        return Result.ok(authUserDomainService.register(authUserBO));
    }


    /**
     * 修改用户信息
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConvert.instance.convertToBoAuthUser(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新用户信息失败");
        }
    }

    /**
     * 更改状态
     * @param authUserDTO
     * @return
     */
    @PostMapping("/changeStatus")
    public Result<Boolean> changegStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            AuthUserBO authUserBO = AuthUserDTOConvert.instance.convertToBoAuthUser(authUserDTO);
            return Result.ok(authUserDomainService.changeStatus(authUserBO));
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新用户状态失败");
        }
    }

    /**
     * 删除用户
     * @param authUserDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteUser(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            AuthUserBO authUserBO = AuthUserDTOConvert.instance.convertToBoAuthUser(authUserDTO);
            return Result.ok(authUserDomainService.deleteUser(authUserBO));
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新用户状态失败");
        }
    }

    @PostMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.getUserInfo.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConvert.instance.convertToBoAuthUser(authUserDTO);
            AuthUserBO userInfo = authUserDomainService.getUserInfo(authUserBO);
            return Result.ok(AuthUserDTOConvert.instance.convertToDTOAuthUser(userInfo));
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新用户信息失败");
        }
    }
}
